package com.yanqu.road.server.gameplayer.module.activity.tomb.cmd;


import com.yanqu.road.entity.activity.ActivityInfo;
import com.yanqu.road.entity.config.goods.GoodsInfo;
import com.yanqu.road.entity.enums.eLogMoneyType;
import com.yanqu.road.entity.log.LogTombUserEnergy;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.pb.tomb.Tomb;
import com.yanqu.road.server.cmd.base.AbstractCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.activity.goodsLimit.GoodsLimitModule;
import com.yanqu.road.server.gameplayer.module.activity.tomb.TombMgr;
import com.yanqu.road.server.gameplayer.module.activity.tomb.TombModule;
import com.yanqu.road.server.gameplayer.module.player.CurrencyModule;
import com.yanqu.road.server.manger.activity.dunhuangshiku.DunHuangShiKuMgr;
import com.yanqu.road.server.manger.config.GoodsMgr;
import com.yanqu.road.server.manger.log.AutoLogMgr;
import com.yanqu.road.server.manger.player.GamePlayerMgr;
import com.yanqu.road.server.protocol.ClientProtocol;
import com.yanqu.road.server.protocol.CrossProtocol;
import com.yanqu.road.server.protocol.GameErrorCode;
import com.yanqu.road.server.protocol.GameProtocol;
import com.yanqu.road.sync.message.SyncResponse;
import com.yanqu.road.utils.property.Property;
import com.yanqu.road.utils.property.PropertyHelper;


import java.math.BigInteger;
import java.util.Date;
import java.util.concurrent.CompletableFuture;

@Cmd(code = GameProtocol.S_TOMB_USE_ENERGY_PACKAGE, desc = "使用体力包")
public class TombUseEnergyPackage extends AbstractCommand {

    private Tomb.TombUseEnergyPackageReq reqMsg;

    private Tomb.TombUserEnergyPackageResp.Builder respBuilder;

    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        this.reqMsg = Tomb.TombUseEnergyPackageReq.parseFrom(packet.getBytes());
        return true;
    }

    @Override
    public void execute(GamePlayer player, YanQuMessage packet) throws Exception {
        this.respBuilder = Tomb.TombUserEnergyPackageResp.newBuilder();
        ActivityInfo activityInfo = TombMgr.getActivityInfo();
        if (activityInfo == null) {
            this.respBuilder.setRet(GameErrorCode.E_ACTIVITY_NOT_OPEN);
            player.sendPacket(ClientProtocol.U_TOMB_USE_ENERGY_PACKAGE, this.respBuilder);
            return;
        }

        int ret = player.getModule(TombModule.class).isOpen(false);
        if (ret > 0) {
            this.respBuilder.setRet(ret);
            player.sendPacket(ClientProtocol.U_TOMB_USE_ENERGY_PACKAGE, this.respBuilder);
            return;
        }

//        int i = TombMgr.getConfig().getConfig().getTOMB_ENERGY_ITEM_ID();
        int i = reqMsg.getGoodsId();
        GoodsInfo goodsInfo = GoodsMgr.getGoodsById(i);
        if (goodsInfo == null) {
            respBuilder.setRet(GameErrorCode.E_GOODS_NO_EXIST);
            return;
        }

        Property property = null;
        // 判断下有没有达到上限
        if (TombMgr.getConfig().getConfig().getSHOW_LIMITED_USE_STRENGTH_ITEM_ID() != null && TombMgr.getConfig().getConfig().getSHOW_LIMITED_USE_STRENGTH_ITEM_ID().contains(reqMsg.getGoodsId())) {
            // 如果是限制道具,先判断上限
            long goodsUseCount = player.getModule(GoodsLimitModule.class).getGoodsUseCount(reqMsg.getGoodsId(), activityInfo.getActivityId());
            if (TombMgr.getShowLimitedUseNum(reqMsg.getGoodsId()) != -1 && goodsUseCount + reqMsg.getNum() > TombMgr.getShowLimitedUseNum(reqMsg.getGoodsId())) {
                // 超过限制
                respBuilder.setRet(GameErrorCode.E_BAG_USE_GOODS_LIMIT);
                return;
            }
            property = new Property(reqMsg.getGoodsId(), BigInteger.valueOf(reqMsg.getNum()));
        } else if (reqMsg.getGoodsId() == TombMgr.getConfig().getConfig().getTOMB_ENERGY_ITEM_ID()) {
            property = new Property(reqMsg.getGoodsId(), BigInteger.valueOf(reqMsg.getNum()));
        } else {
            respBuilder.setRet(GameErrorCode.E_ERROR_PARAMETER);
            return;
        }

        if (!player.getModule(CurrencyModule.class).removeCurrency(property, eLogMoneyType.Tomb, eLogMoneyType.TombUseEnergyPackage)) {
            respBuilder.setRet(GameErrorCode.E_BAG_ITEM_NO_ENOUGH);
            return;
        }
        if (TombMgr.getConfig().getConfig().getSHOW_LIMITED_USE_STRENGTH_ITEM_ID() != null && TombMgr.getConfig().getConfig().getSHOW_LIMITED_USE_STRENGTH_ITEM_ID().contains(reqMsg.getGoodsId())) {
            // 限制道具记录一下使用的数量
            player.getModule(GoodsLimitModule.class).updateGoodsUseCount(reqMsg.getGoodsId(), activityInfo.getActivityId(), reqMsg.getNum());
        }

        Tomb.TombCrossUserAddItemReq.Builder builder = Tomb.TombCrossUserAddItemReq.newBuilder();
        builder.setAddEnergy(reqMsg.getNum() * goodsInfo.getParamList().get(0).intValue());
        Tomb.TombCrossUserAddItemResp.Builder builder1 = Tomb.TombCrossUserAddItemResp.newBuilder();
        CompletableFuture<SyncResponse> future = GamePlayerMgr.sendToActivityCrossServerForUnionGroup(player.getUserId(), CrossProtocol.C_TOMB_CROSS_ADD_ITEM, activityInfo.getActivityId(), builder, builder1);
        future.thenAccept(syncResponse -> {
            if (!this.respBuilder.hasRet()) {
                this.respBuilder.setRet(0);
            }
            this.respBuilder.setEnergy(builder1.getEnergy());
            player.sendPacket(ClientProtocol.U_TOMB_USE_ENERGY_PACKAGE, this.respBuilder);

            LogTombUserEnergy l = new LogTombUserEnergy();
            l.setUserId(player.getUserId());
            l.setActivityId(TombMgr.getActivityInfo().getActivityId());
            l.setUnionUid(player.getUserInfo().getUnionUid());
            l.setEnergyChange(builder.getAddEnergy());
            l.setEnergy(builder1.getEnergy());
            l.setDescription(String.format("使用了%d个能力包，增加了%d能力", reqMsg.getNum(), builder.getAddEnergy()));
            AutoLogMgr.add(l);
        });
    }

    @Override
    public void sendData(GamePlayer player) {

    }

    @Override
    public boolean isDebug() {
        return true;
    }

    @Override
    public String getRequestData() {
        return this.reqMsg.toString();
    }
}
